package com.maozw.api.gateway.route;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author MAOZW
 * @Description: DymicRouteLocator
 * @date 2018/8/10 15:43
 */

public class DymicRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
    private static final Logger LOGGER = LoggerFactory.getLogger(DymicRouteLocator.class);

    private JdbcTemplate jdbcTemplate;

    private RedisTemplate redisTemplate;

    private ZuulProperties properties;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public DymicRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
        this.properties = properties;
        LOGGER.info("servletPath:{}", servletPath);
    }

    /**
     * 可以捕捉Spring的事件发布，可以动态更新路由，也可以手动去刷新路由（默认会捕获eureka30 秒心跳事件）
     */
    @Override
    public void refresh() {
        super.doRefresh();
    }

    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        LOGGER.info("servletPath:{}", "=======================locateRoutes=====================");
        LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<String, ZuulProperties.ZuulRoute>();
        //从application.properties中加载路由信息
        routesMap.putAll(super.locateRoutes());
        //从db中加载路由信息
        LoadRouteFromDB loadRouteFromDB  = new LoadRouteFromDB();
        Map<String, ZuulProperties.ZuulRoute> stringZuulRouteMap = loadRouteFromDB.loadRoute(jdbcTemplate);
        //routesMap.putAll(loadRoute.loadRoute());
        routesMap.putAll(stringZuulRouteMap);
        LinkedHashMap<String, ZuulProperties.ZuulRoute> values = new LinkedHashMap<>();
        for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
            String path = entry.getKey();
            // Prepend with slash if not already present.
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            if (StringUtils.hasText(this.properties.getPrefix())) {
                path = this.properties.getPrefix() + path;
                if (!path.startsWith("/")) {
                    path = "/" + path;
                }
            }
            values.put(path, entry.getValue());
        }
        return values;
    }

//    /**
//     * Ignored route paths (or patterns), if any.
//     */
//    @Override
//    public Collection<String> getIgnoredPaths() {
//        return null;
//    }

    /**
     * A map of route path (pattern) to location (e.g. service id or URL).
     */
//    @Override
//    public List<Route> getRoutes() {
//        return null;
//    }

    /**
     * Maps a path to an actual route with full metadata.
     *
     * @param path
     */
//    @Override
//    public Route getMatchingRoute(String path) {
//        return null;
//    }
}
